﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_Vet
    {
        public bool CreateOneVet(Vet_Info objVet_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Vet_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"VetID", objVet_Info.VetID));
            command.Parameters.Add(new SqlParameter(@"MAFFLicenceID", objVet_Info.MAFFLicenceID));
            command.Parameters.Add(new SqlParameter(@"UserName", objVet_Info.UserName));
            command.Parameters.Add(new SqlParameter(@"Password", objVet_Info.Password));
            command.Parameters.Add(new SqlParameter(@"VetName", objVet_Info.VetName));
            command.Parameters.Add(new SqlParameter(@"Address", objVet_Info.Address));
            command.Parameters.Add(new SqlParameter(@"DOB", objVet_Info.DOB));
            command.Parameters.Add(new SqlParameter(@"PhoneNumber", objVet_Info.PhoneNumber));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneVet(Vet_Info objVet_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Vet_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@VetID", objVet_Info.VetID));
            command.Parameters.Add(new SqlParameter("@MAFFLicenceID", objVet_Info.MAFFLicenceID));
            command.Parameters.Add(new SqlParameter("@UserName", objVet_Info.UserName));
            command.Parameters.Add(new SqlParameter("@Password", objVet_Info.Password));
            command.Parameters.Add(new SqlParameter("@VetName", objVet_Info.VetName));
            command.Parameters.Add(new SqlParameter("@Address", objVet_Info.Address));
            command.Parameters.Add(new SqlParameter("@DOB", objVet_Info.DOB));
            command.Parameters.Add(new SqlParameter("@PhoneNumber", objVet_Info.PhoneNumber));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneVet(int VetID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Vet_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"VetID", VetID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllVet()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Vet_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public Vet_Info GetOneVet(int VetID)
        {
            Vet_Info objVet_Info = new Vet_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Vet_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"VetID", VetID));
            try
            {
                objConnection.Open();
                SqlDataReader ordVet = command.ExecuteReader(); //OjectRecordDatareader
                ordVet.Read();
                if (ordVet.HasRows)
                {
                    if (ordVet["VetID"] != DBNull.Value)
                    {
                        objVet_Info.VetID = Convert.ToInt16(ordVet["VetID"]);
                    }
                    if (ordVet["MAFFLicenceID"] != DBNull.Value)
                    {
                        objVet_Info.MAFFLicenceID = Convert.ToString(ordVet["MAFFLicenceID"]);
                    }
                    if (ordVet["UserName"] != DBNull.Value)
                    {
                        objVet_Info.UserName = Convert.ToString(ordVet["UserName"]);
                    }
                    if (ordVet["Password"] != DBNull.Value)
                    {
                        objVet_Info.Password = Convert.ToString(ordVet["Password"]);
                    }
                    if (ordVet["VetName"] != DBNull.Value)
                    {
                        objVet_Info.VetName = Convert.ToString(ordVet["VetName"]);
                    }
                    if (ordVet["Address"] != DBNull.Value)
                    {
                        objVet_Info.Address = Convert.ToString(ordVet["Address"]);
                    }
                    if (ordVet["DOB"] != DBNull.Value)
                    {
                        objVet_Info.DOB = Convert.ToDateTime(ordVet["DOB"]);
                    }
                    if (ordVet["PhoneNumber"] != DBNull.Value)
                    {
                        objVet_Info.PhoneNumber = Convert.ToString(ordVet["PhoneNumber"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objVet_Info;
        }
    }
}
